<template xmlns="http://www.w3.org/1999/html">
  <div class="content">
    <div class="title">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>系统</b></el-breadcrumb-item>
        <el-breadcrumb-item>账号管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="main-content">
      <el-form :inline="true" :model="condition">&nbsp;&nbsp;&nbsp;&nbsp;
        <span>搜索</span>&nbsp;&nbsp;
        <el-input v-model="condition.input" placeholder="姓名/手机号" style="width: 200px"></el-input>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span>部门</span>&nbsp;&nbsp;
        <el-form-item >
          <el-cascader 
            :options="deptOptions"
            @active-item-change="handleDeptChange"
            v-model="condition.selectedDept">  
          </el-cascader>
        </el-form-item>&nbsp;&nbsp;
        <el-form-item>
          <el-button type="primary" @click="queryAccount">查询</el-button>
          <el-button @click="add" type="primary">新增</el-button>
        </el-form-item>
      </el-form>
      
        <el-table :data="accounts" highlight-current-row v-loading="loading">
          <el-table-column prop="staffId" label="员工ID" width="100">
          </el-table-column>
          <el-table-column prop="staffName" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="email" label="邮箱" width="200">
          </el-table-column>
          <el-table-column prop="phone" label="联系电话" width="130">
          </el-table-column>
          <el-table-column prop="deptName" label="部门" width="140" >
          </el-table-column>
          <el-table-column prop="roleArray" label="角色" width="140" >
          </el-table-column>
          <el-table-column prop="options" label="操作" min-width="180">
            <template scope="scope">
              <el-button @click="change(scope.$index,scope.row)" type="text" size="small">修改</el-button>
              <el-button @click="changeState(scope.$index,scope.row)" type="text" size="small">{{scope.row.status=='0'?'禁用':'启用'}}</el-button>
              <el-button @click="changePwd(scope.$index,scope.row)" type="text" size="small">重置密码</el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <el-pagination layout="sizes,prev, pager, next" @current-change="getCurrentPage"
                       :page-sizes="[10,20,30,50]" :total="total"
                       :page-size="pagesize"
                       :current-page.sync="currentPage"
                       @size-change="sizeChange"
                       style="text-align: center;margin-top: 20px">
        </el-pagination>
     
    </div>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      size="tiny"
      :before-close="handleClose">
      <span>确认重置密码</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitRequest">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import ElCol from 'element-ui/packages/col/src/col'
  import ElForm from '../../../node_modules/element-ui/packages/form/src/form.vue'
  import axios from 'axios'
  import $ from 'jquery'

  export default {
    components: {
      ElForm,
      ElCol},
    data () {
      return {
        //state:'停用',
        currentRow:'',
        dialogVisible:false,
        condition:{input:'',selectedDept:''},
        companyNo:sessionStorage.getItem('companyNo'),
        deptOptions:[],
        accounts:[],
        pagesize: 10,
        currentPage:1,
        total: 10,
        recive:[],
      }
    },
    methods:{
      //页面加载时获取账号信息
      getAccount:function () {
        this.$http.get("/staff/list",{
          params:{
            pageSize:this.pagesize,
            pageNo:this.currentPage,
          }
        }).then((response)=>{
          let that = this
          $.each(response.data.data , function(index , value){
            var roleArray2=[]
            $.each(value.roleArray , function(inde , val){
              //that.recive.roleArray.push(val.roleName)
              roleArray2.push(val.roleName+'   ')
            })
            value.roleArray=roleArray2
          })
          //console.log("accounts:"+JSON.stringify(this.accounts.options))
          this.accounts = response.data.data;
          this.total=response.data.totalCount
        }).catch(function (error) {
          console.log(error)
        })
      },

      change(index,row){
        this.$router.push({path:'/backgroundManaSys/addAccount',query:{content:row.staffId}})
       /* this.$http.get('/staff/look/'+row.staffId,
        ).then((response)=>{
          console.log(response.data.data[0])
          
        }).catch(function (response) {
          console.log(response)
        })*/
      },

      handleDeptChange(Dept){
        var i 
        i = Dept.length
        this.condition.selectedDept = Dept
        setTimeout(_=>{
          this.test(Dept[i-1],this.deptOptions)
        },300)
      },

      test(deptId , options){
        let that = this
        $.each(options,function(index,value){
          if(value.value!=deptId&value.children!=''){
            that.test(deptId,value.children)
          }
          if(value.value==deptId){
            that.$http.get("/dept/list",{
              params:{
                parentDeptId:deptId,
                companyNo:that.companyNo
              }
            })
            .then((response)=>{
              value.children=[]
              $.each(response.data.data,function(index1,value1){
                value.children.push({value:value1.deptId,label:value1.deptName,children:[]})
                console.log("value:"+JSON.stringify(value))
              })
            })
          }
        })
      },

      getDeptOptions(id){
        this.$http.get("/dept/list",{
          params:{
            parentDeptId:id,
            companyNo:this.companyNo
          }
        })
        .then((response)=>{
          let that = this
          $.each(response.data.data , function(index , value){
            if(id===''){
              that.deptOptions.push({value:value.deptId,label:value.deptName,children:[]})
            }
          })
        })
      },


      getCurrentPage:function (currentPage) {
        this.currentPage = currentPage;
        this.getAccount();
      },

      queryAccount:function () {
        var staffListForm = {}
        var i
        i = this.condition.selectedDept.length
        this.$http.get("/staff/list",{
          params:{
            keyword:this.condition.input,
            pageSize:this.pagesize,
            pageNo:this.currentPage,
            deptId:this.condition.selectedDept[i-1]
          }
        })
        .then((response)=>{
          let that = this
          $.each(response.data.data , function(index , value){
            var roleArray2=[]
            $.each(value.roleArray , function(inde , val){
              roleArray2.push(val.roleName+' ')
            })
            value.roleArray=roleArray2
          })
          console.log("accounts:"+response.data.data[0])
          this.accounts = response.data.data;
          this.total=response.data.totalCount
        })
        .catch(function(error){
          console.log(error)
        })
      },

      changeState(index , row){
        let that = this
        this.$http.post("/staff/startOrStopStaff",{
          staffId:row.staffId,
          status:row.status
        })
        .then((response)=>{
          console.log("row:"+JSON.stringify(row))
          if(response.data.success){
            console.log("状态修改成功")
            that.$message("状态修改成功")
            that.getAccount()
          }else{
            that.$message("状态修改失败")
          }
        })
        .catch(function(error){
          console.log(error)
        })
      },

      changePwd(index , row){
        this.dialogVisible = true
        this.currentRow = row
      },

      submitRequest(){
        let that = this
        this.$http.get("/staff/resetPassword/"+this.currentRow.staffId)
        .then((response)=>{
          if(response.data.success){
            that.dialogVisible = false
            that.$message("重置成功")
          }else{
            that.$message("重置失败")
          }
        })
        .catch(function(error){
          console.log(error)
        })
      },

      sizeChange(val){
        this.pagesize=val;
        this.getAccount();
      },

      add:function () {
        this.$router.push({path:'/backgroundManaSys/addAccount'})
      },

    },

    mounted() {
      this.getAccount();
      this.getDeptOptions('')
    },
    beforeMount(){
      this.getAccount()
    }
  }
</script>

<style lang="scss" scoped>
.content {
  padding: 10px;
  background: #F7F7F7;
  .title {
    color: #99A9BF;
    font-size: 14px;
    line-height: 18px;
    margin: 8px 0px 10px 0px;
  }
  .main-content {
    background: #fff;
    position: fixed;
    top: 96px;
    bottom: 10px;
    left: 216px;
    right: 16px;
    overflow: auto;
    padding: 20px;
  }
}
</style>
